package org.pluslibs.framework.swingplus.utils;

import java.lang.reflect.InvocationTargetException;

import javax.swing.SwingUtilities;

/**
 * GUI utilites; centers components to screen mainly
 */
public class SwingEventThreadUtils {

    /* *********************************************** */
    // Run in swing thread
	
    public static void invokeAndWait_NoException(Runnable runnable) {
        try {
            SwingUtilities.invokeAndWait(runnable);
        } catch ( InterruptedException e ) {
        	throw new RuntimeException(e);
        } catch ( InvocationTargetException e ) {
        	throw new RuntimeException(e);
        }
    }
    
    /* *********************************************** */
    // Ensure run in Swing Thread
    
    public static void ensureSwingThread(Runnable runnable) throws InterruptedException, InvocationTargetException {
        if ( SwingUtilities.isEventDispatchThread() ) {
            runnable.run();
        } else {
            SwingUtilities.invokeAndWait(runnable);
        }
    }
    public static void ensureSwingThread_InvokeLater(Runnable runnable) {
        if ( SwingUtilities.isEventDispatchThread() ) {
            runnable.run();
        } else {
            SwingUtilities.invokeLater(runnable);
        }
    }
    
    /* *********************************************** */
}
